{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-18T08:40:02.586737Z",
     "start_time": "2025-09-18T08:39:59.913086Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from itertools import chain\n",
    "from tqdm import trange,tqdm\n",
    "from src.tools.utils import write_json"
   ],
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-18T08:35:36.880367Z",
     "start_time": "2025-09-18T08:35:30.260213Z"
    }
   },
   "cell_type": "code",
   "source": "df = pd.read_pickle('../data/dataset/trx_time_targets.pkl')",
   "id": "62967ed46c29b607",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-18T08:35:36.936862Z",
     "start_time": "2025-09-18T08:35:36.889344Z"
    }
   },
   "cell_type": "code",
   "source": "df",
   "id": "8515fd8b0785fde6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                                                 client_id  \\\n",
       "0        06d777b861a65be029fd03851af46fb9c3ef4391714436...   \n",
       "1        06d777b861a65be029fd03851af46fb9c3ef4391714436...   \n",
       "2        06d777b861a65be029fd03851af46fb9c3ef4391714436...   \n",
       "3        06d777b861a65be029fd03851af46fb9c3ef4391714436...   \n",
       "4        06d777b861a65be029fd03851af46fb9c3ef4391714436...   \n",
       "...                                                    ...   \n",
       "1125338  fff885cf797bf5e8f79c8319e2b512f08875105ba2105e...   \n",
       "1125339  fff885cf797bf5e8f79c8319e2b512f08875105ba2105e...   \n",
       "1125340  fff885cf797bf5e8f79c8319e2b512f08875105ba2105e...   \n",
       "1125341  fff885cf797bf5e8f79c8319e2b512f08875105ba2105e...   \n",
       "1125342  fff885cf797bf5e8f79c8319e2b512f08875105ba2105e...   \n",
       "\n",
       "                                                event_time  \\\n",
       "0                                 [1634136341, 1638718103]   \n",
       "1                                 [1634136341, 1638718103]   \n",
       "2                                 [1634136341, 1638718103]   \n",
       "3                                 [1634136341, 1638718103]   \n",
       "4                                 [1634136341, 1638718103]   \n",
       "...                                                    ...   \n",
       "1125338  [1609660447, 1610090831, 1611184783, 161136355...   \n",
       "1125339  [1609660447, 1610090831, 1611184783, 161136355...   \n",
       "1125340  [1609660447, 1610090831, 1611184783, 161136355...   \n",
       "1125341  [1609660447, 1610090831, 1611184783, 161136355...   \n",
       "1125342  [1609660447, 1610090831, 1611184783, 161136355...   \n",
       "\n",
       "                                             event_subtype         mon  \\\n",
       "0                                                   [1, 1]  1645977600   \n",
       "1                                                   [1, 1]  1648656000   \n",
       "2                                                   [1, 1]  1651248000   \n",
       "3                                                   [1, 1]  1653926400   \n",
       "4                                                   [1, 1]  1656518400   \n",
       "...                                                    ...         ...   \n",
       "1125338  [7, 7, 10, 3, 1, 3, 11, 7, 3, 12, 43, 1, 3, 10...  1664467200   \n",
       "1125339  [7, 7, 10, 3, 1, 3, 11, 7, 3, 12, 43, 1, 3, 10...  1667145600   \n",
       "1125340  [7, 7, 10, 3, 1, 3, 11, 7, 3, 12, 43, 1, 3, 10...  1669737600   \n",
       "1125341  [7, 7, 10, 3, 1, 3, 11, 7, 3, 12, 43, 1, 3, 10...  1672416000   \n",
       "1125342  [7, 7, 10, 3, 1, 3, 11, 7, 3, 12, 43, 1, 3, 10...  1675094400   \n",
       "\n",
       "         target_1  target_2  target_3  target_4  \n",
       "0               0         0         0         0  \n",
       "1               0         0         0         0  \n",
       "2               0         0         0         0  \n",
       "3               0         0         0         0  \n",
       "4               0         0         0         0  \n",
       "...           ...       ...       ...       ...  \n",
       "1125338         0         0         0         0  \n",
       "1125339         0         0         0         0  \n",
       "1125340         0         0         0         0  \n",
       "1125341         0         0         0         0  \n",
       "1125342         0         0         0         0  \n",
       "\n",
       "[1125343 rows x 8 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>client_id</th>\n",
       "      <th>event_time</th>\n",
       "      <th>event_subtype</th>\n",
       "      <th>mon</th>\n",
       "      <th>target_1</th>\n",
       "      <th>target_2</th>\n",
       "      <th>target_3</th>\n",
       "      <th>target_4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>06d777b861a65be029fd03851af46fb9c3ef4391714436...</td>\n",
       "      <td>[1634136341, 1638718103]</td>\n",
       "      <td>[1, 1]</td>\n",
       "      <td>1645977600</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>06d777b861a65be029fd03851af46fb9c3ef4391714436...</td>\n",
       "      <td>[1634136341, 1638718103]</td>\n",
       "      <td>[1, 1]</td>\n",
       "      <td>1648656000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>06d777b861a65be029fd03851af46fb9c3ef4391714436...</td>\n",
       "      <td>[1634136341, 1638718103]</td>\n",
       "      <td>[1, 1]</td>\n",
       "      <td>1651248000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>06d777b861a65be029fd03851af46fb9c3ef4391714436...</td>\n",
       "      <td>[1634136341, 1638718103]</td>\n",
       "      <td>[1, 1]</td>\n",
       "      <td>1653926400</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>06d777b861a65be029fd03851af46fb9c3ef4391714436...</td>\n",
       "      <td>[1634136341, 1638718103]</td>\n",
       "      <td>[1, 1]</td>\n",
       "      <td>1656518400</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1125338</th>\n",
       "      <td>fff885cf797bf5e8f79c8319e2b512f08875105ba2105e...</td>\n",
       "      <td>[1609660447, 1610090831, 1611184783, 161136355...</td>\n",
       "      <td>[7, 7, 10, 3, 1, 3, 11, 7, 3, 12, 43, 1, 3, 10...</td>\n",
       "      <td>1664467200</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1125339</th>\n",
       "      <td>fff885cf797bf5e8f79c8319e2b512f08875105ba2105e...</td>\n",
       "      <td>[1609660447, 1610090831, 1611184783, 161136355...</td>\n",
       "      <td>[7, 7, 10, 3, 1, 3, 11, 7, 3, 12, 43, 1, 3, 10...</td>\n",
       "      <td>1667145600</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1125340</th>\n",
       "      <td>fff885cf797bf5e8f79c8319e2b512f08875105ba2105e...</td>\n",
       "      <td>[1609660447, 1610090831, 1611184783, 161136355...</td>\n",
       "      <td>[7, 7, 10, 3, 1, 3, 11, 7, 3, 12, 43, 1, 3, 10...</td>\n",
       "      <td>1669737600</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1125341</th>\n",
       "      <td>fff885cf797bf5e8f79c8319e2b512f08875105ba2105e...</td>\n",
       "      <td>[1609660447, 1610090831, 1611184783, 161136355...</td>\n",
       "      <td>[7, 7, 10, 3, 1, 3, 11, 7, 3, 12, 43, 1, 3, 10...</td>\n",
       "      <td>1672416000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1125342</th>\n",
       "      <td>fff885cf797bf5e8f79c8319e2b512f08875105ba2105e...</td>\n",
       "      <td>[1609660447, 1610090831, 1611184783, 161136355...</td>\n",
       "      <td>[7, 7, 10, 3, 1, 3, 11, 7, 3, 12, 43, 1, 3, 10...</td>\n",
       "      <td>1675094400</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1125343 rows × 8 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-18T08:38:18.076081Z",
     "start_time": "2025-09-18T08:38:18.060160Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 统计每个序列特征的特异值数量和具体数值\n",
    "def calc_col_unique(df: pd.DataFrame, col: str):\n",
    "    new_vals=df[col].apply(lambda x:list(set(list(x)))).values.tolist()\n",
    "    new_vals=list(set(chain.from_iterable(new_vals)))\n",
    "    num=len(new_vals)\n",
    "    min_=int(min(new_vals))\n",
    "    max_=int(max(new_vals))\n",
    "    vocab_size=max_+4\n",
    "    return {\n",
    "        \"num\":num,\n",
    "        \"min\":min_,\n",
    "        \"max\":max_,\n",
    "        \"vocab_size\":vocab_size,\n",
    "        \"mask_token_id\":vocab_size+1,\n",
    "        \"cls_token_id\":vocab_size+2,\n",
    "        \"sep_token_id\":vocab_size+3,\n",
    "    }"
   ],
   "id": "a2ce78320c66047c",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-18T08:38:44.582193Z",
     "start_time": "2025-09-18T08:38:20.125618Z"
    }
   },
   "cell_type": "code",
   "source": [
    "trx_dict = {}\n",
    "for col in tqdm(['event_subtype']):\n",
    "    trx_dict[col]=calc_col_unique(df, col)\n"
   ],
   "id": "a1c4491494a85673",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [00:24<00:00, 24.43s/it]\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-18T08:39:03.670351Z",
     "start_time": "2025-09-18T08:39:03.656593Z"
    }
   },
   "cell_type": "code",
   "source": "trx_dict",
   "id": "33e11e73657de0d0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'event_subtype': {'num': 59,\n",
       "  'min': 1,\n",
       "  'max': 61,\n",
       "  'vocab_size': 65,\n",
       "  'mask_token_id': 66,\n",
       "  'cls_token_id': 67,\n",
       "  'sep_token_id': 68}}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-18T08:40:16.594945Z",
     "start_time": "2025-09-18T08:40:16.579162Z"
    }
   },
   "cell_type": "code",
   "source": "write_json(trx_dict,\"../data/dataset/trx_unique.json\")",
   "id": "2a24f4ccc4dd2f95",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "finish dump json str to path: ../data/dataset/trx_unique.json\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-19T08:06:22.843282Z",
     "start_time": "2025-09-19T08:06:21.887241Z"
    }
   },
   "cell_type": "code",
   "source": "import pandas as pd",
   "id": "b9a9dd20f69bfaf9",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-19T08:07:12.888151Z",
     "start_time": "2025-09-19T08:07:05.608615Z"
    }
   },
   "cell_type": "code",
   "source": "df = pd.read_pickle('../data/dataset/mini_trx_time_targets.pkl')",
   "id": "72c29f770a5685e9",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-19T08:09:05.862755Z",
     "start_time": "2025-09-19T08:09:05.786843Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_sample = df.sample(frac=0.001, random_state=42)\n",
    "df_sample.to_pickle('../data/dataset/mini_trx_sample_0001.pkl')"
   ],
   "id": "54bcea9b4cdfb4c9",
   "outputs": [],
   "execution_count": 3
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
